package models

import (
	"context"
	"github.com/cy77cc/hioshop/global"
	"time"
)

type Notice struct {
	Model
	Content  string `json:"content" gorm:"type:varchar(255);default:'0'"`
	EndTime  int    `json:"end_time" gorm:"type:int"`
	IsDelete int8   `json:"is_delete" gorm:"type:tinyint"`
}

func (n *Notice) GetNotices() (list []string, err error) {
	ctx := context.Background()
	sMembers := global.REDIS.SMembers(ctx, "notices")
	// 缓存没有换数据库
	if len(sMembers.Val()) == 0 {
		err = global.DB.Model(&Notice{}).Select("content").Where("is_delete=?", 0).Find(&list).Error
		for _, v := range list {
			global.REDIS.SAdd(ctx, "notices", v)
		}
	}
	global.REDIS.Expire(ctx, "notices", time.Hour)
	err = sMembers.ScanSlice(&list)
	return
}
